package com.ruoyi.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.SysUserAccount;
import com.ruoyi.system.domain.SysUserLabel;
import com.ruoyi.system.mapper.SysUserAccountMapper;
import com.ruoyi.system.mapper.SysUserLabelMapper;
import com.ruoyi.system.service.ISysUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户账号Service业务层处理
 *
 * @author felix
 * @date 2022-04-25
 */
@Service
public class SysUserAccountServiceImpl implements ISysUserAccountService {
    @Autowired
    private SysUserAccountMapper sysUserAccountMapper;

    @Autowired
    private SysUserLabelMapper userLabelMapper;

    /**
     * 查询用户账号
     *
     * @param id 用户账号ID
     * @return 用户账号
     */
    @Override
    public SysUserAccount selectSysUserAccountById(String id) {
        SysUserAccount result = sysUserAccountMapper.selectSysUserAccountById(id);
        if(StringUtils.isNotEmpty(result.getUserId())){
            String[] labels = userLabelMapper.selectSysUserLabelByUserId(Long.parseLong(result.getUserId()));
            result.setModuleLabels(labels);
        }

        return result;
    }

    /**
     * 查询用户账号列表
     *
     * @param sysUserAccount 用户账号
     * @return 用户账号
     */
    @Override
    public List<SysUserAccount> selectSysUserAccountList(SysUserAccount sysUserAccount) {
        return sysUserAccountMapper.selectSysUserAccountList(sysUserAccount);
    }

    /**
     * 新增用户账号
     *
     * @param sysUserAccount 用户账号
     * @return 结果
     */
    @Override
    public int insertSysUserAccount(SysUserAccount sysUserAccount) {
        sysUserAccount.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        sysUserAccount.setCreateTime(DateUtils.getNowDate());
        sysUserAccount.setCreateBy(SecurityUtils.getUsername());
        return sysUserAccountMapper.insertSysUserAccount(sysUserAccount);
    }

    /**
     * 修改用户账号
     *
     * @param sysUserAccount 用户账号
     * @return 结果
     */
    @Override
    public int updateSysUserAccount(SysUserAccount sysUserAccount) {
        sysUserAccount.setUpdateTime(DateUtils.getNowDate());
        sysUserAccount.setUpdateBy(SecurityUtils.getUsername());
        if(StringUtils.isNotEmpty(sysUserAccount.getUserId())){
            // 删除标签组，
            Long userId = Long.parseLong(sysUserAccount.getUserId());
            userLabelMapper.deleteSysUserLabelById(userId);
            // 批量插入标签组
            insertUserLabel(sysUserAccount);
        }

        return sysUserAccountMapper.updateSysUserAccount(sysUserAccount);
    }

    public void insertUserLabel(SysUserAccount user) {
        String[] labels = user.getModuleLabels();
        Long userId = Long.parseLong(user.getUserId());
        if (StringUtils.isNotNull(labels)) {
            // 新增用户与角色管理
            List<SysUserLabel> list = new ArrayList<SysUserLabel>();
            for (String label : labels) {
                SysUserLabel ur = new SysUserLabel();
                ur.setUserId(userId);
                ur.setModuleLabel(label);
                list.add(ur);
            }
            if (list.size() > 0) {
                userLabelMapper.batchUserLabel(list);
            }
        }
    }

    /**
     * 批量删除用户账号
     *
     * @param ids 需要删除的用户账号ID
     * @return 结果
     */
    @Override
    public int deleteSysUserAccountByIds(String[] ids) {
        return sysUserAccountMapper.deleteSysUserAccountByIds(ids);
    }

    /**
     * 删除用户账号信息
     *
     * @param id 用户账号ID
     * @return 结果
     */
    @Override
    public int deleteSysUserAccountById(String id) {
        return sysUserAccountMapper.deleteSysUserAccountById(id);
    }

    @Override
    public List<SysUserAccount> selectUserAccountByUserList(List<String> users) {
        return sysUserAccountMapper.selectUserAccountByUserList(users);
    }

    @Override
    public List<SysUserAccount> selectUserAccountByAliasList(List<String> alias) {
        return sysUserAccountMapper.selectUserAccountByAliasList(alias);
    }

    @Override
    public SysUserAccount selectSysUserAccountByWxId(String wxId) {
        return sysUserAccountMapper.selectSysUserAccountByWxId(wxId);
    }

    @Override
    public SysUserAccount selectSysUserAccountByPhone(String phoneNumber) {
        return sysUserAccountMapper.selectSysUserAccountByPhone(phoneNumber);
    }

    @Override
    public int updateUserAccountCid(String cid, String uid) {

        return sysUserAccountMapper.updateUserAccountCid(cid,uid);
    }
}
